var api = require("../../api.js");
var app = getApp();
Page({
  data: {
    user_info: null,
    token:null,
    tk_id: 1,
  },

  //  主动登陆授权 返回用户信息
  getUserInfoHandle(event) {
    var page = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code;
          var jump = "/pages/index/index";
          wx.showLoading({
            title: '正在加载',
          });
          // 获取授权
          wx.getUserInfo({
            
            success: function (res) {
              app.request({
                url: api.passport.login,
                method: "POST",
                data: {
                  code: code,
                  user_info: res.rawData,
                  encrypted_data: res.encryptedData,
                  iv: res.iv,
                  signature: res.signature,
                  tk_id: page.data.tk_id,
                },
                success: function(res) {
                  wx.hideLoading();
                  if (res.code == 1) {
                    var timestamp = Date.parse(new Date());
                    var expiration = timestamp + 1000*60*60*24*7; //有效期一周
                    wx.setStorageSync("expiration", expiration);
                    wx.setStorageSync("token", res.data.token);
                    wx.setStorageSync("user_info", res.data);
                    app.globalData.Wxuser = res.data;
                    app.globalData.token = res.data.token;
                    var jump2 = wx.getStorageSync("jump");
                    if (jump2){
                      wx.redirectTo({
                        url: jump2,
                      })
                    }else{
                      wx.switchTab({
                        url: jump
                      });
                    }
                    
                  } else {
                    wx.showToast({
                      title: res.msg
                    });
                  }
                },
                complete: function (res) {
                  // console.log(res);
                }
              });
              wx.hideLoading();
            }
          });
        } else {
          // console.log(res);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this
    if (options.tk_id > 1) {
      page.setData({
        tk_id: options.tk_id,
      })
    } else if (decodeURIComponent(options.scene)>1){
      page.setData({ tk_id: decodeURIComponent(options.scene) });
    }

    // 每次刷新  读取本地userInfo
    var timestamp  = Date.parse(new Date());
    var expiration = wx.getStorageSync("expiration");
    var user_info  = wx.getStorageSync("user_info");
    if (user_info && user_info.session_key && expiration > timestamp){
        app.globalData.Wxuser = wx.getStorageSync("user_info");
        app.globalData.token  = wx.getStorageSync("token");
        wx.switchTab({
          url: "/pages/index/index"
        });
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
});